package com.jhws.business.property.bo;

import com.alibaba.fastjson.JSON;
import com.jhws.business.car.bo.*;
import com.jhws.business.car.service.CarInfoService;
import com.jhws.business.car.service.ParkingLotService;
import com.jhws.business.comm.bo.TblCommDimBo;
import com.jhws.business.comm.service.ITblCommDimService;
import com.jhws.business.customer.bo.PayCustomerSecret;
import com.jhws.business.customer.service.PayCustService;
import com.jhws.business.property.service.PropertyFeeService;
import com.jhws.common.pay.bo.IOrderPayModule;
import com.jhws.common.pay.bo.Order;
import com.jhws.common.pay.bo.PayModuleInfo;
import com.jhws.common.pay.service.OrderService;
import com.jhws.core.mns.service.IMNSService;
import com.jhws.util.DateUtil;
import com.jhws.util.LogHelper;
import com.jhws.util.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.*;

/* 物业缴费订单处理模块 */
@Component
public class PropertyFeePayModule implements IOrderPayModule {
    public final static String PROPERTY_FEE = "PROPERTY_FEE"; //物业缴费

    public final static String[] name = {PROPERTY_FEE};

    private final String [] desc = {"物业缴费"};
    @Resource
    private OrderService orderService;
    @Resource
    private ITblCommDimService commDimService;
    @Resource
    private PropertyFeeService propertyFeeService;
    @Resource
    private PayCustService payCustService;

    @Override
    public String[] getModuleName() {
        return name;
    }

    @Override
    public String getPayModuleName() { return PROPERTY_FEE; }

    @Override
    public String [] getModuleDesc() { return desc; }

    @Override
    public void handle(Order order) throws Exception {
        if (order.getOrderType().equals(PROPERTY_FEE)) {
            LogHelper.e("---------->     " + order);
            PropertyFeePrivateInfo propertyFeePrivateInfo = JSON.parseObject(order.getDetail(), PropertyFeePrivateInfo.class);
            propertyFeeService.saveState(order.getAttach(), 1, propertyFeePrivateInfo.getCurrentBillDate());
        }
    }

    @Override
    public boolean preCheck(Order order) {
        LogHelper.d("--------------------> sssss " + order.toString());
        switch (order.getOrderType()) {
            case PROPERTY_FEE: {
                PropertyFeePrivateInfo propertyFeePrivateInfo = JSON.parseObject(order.getDetail(), PropertyFeePrivateInfo.class);
                return !StringUtils.isEmpty(order.getOrderType()) &&
                        !StringUtils.isEmpty(order.getCommCode()) &&
                        !StringUtils.isEmpty(order.getAttach()) &&
                        !StringUtils.isEmpty(order.getBody()) &&
                        !StringUtils.isEmpty(propertyFeePrivateInfo.getCurrentBillDate());
            }
            default: {
                PropertyFeePrivateInfo propertyFeePrivateInfo = JSON.parseObject(order.getDetail(), PropertyFeePrivateInfo.class);
                return !StringUtils.isEmpty(order.getOrderType()) &&
                        !StringUtils.isEmpty(order.getCommCode()) &&
                        !StringUtils.isEmpty(order.getAttach()) &&
                        !StringUtils.isEmpty(order.getBody()) &&
                        !StringUtils.isEmpty(propertyFeePrivateInfo.getCurrentBillDate());
            }
        }
    }

    @Override
    @PostConstruct
    public void registerModule() {
        orderService.registerModule(this);
    }

    @Override
    public PayCustomerSecret findCustomerSecret(Order order) throws Exception {
        TblCommDimBo commDimBo = commDimService.findTblCommDimByCommCode(order.getCommCode());
        return payCustService.findCustSecretByPayType(commDimBo.getPayPropertyCustId(),order.getPayType());
    }

    @Override
    public void setCustomerId(String id, Long customId) throws Exception{
        TblCommDimBo commDimBo = commDimService.findTblCommDimByCommCode(id);
        commDimBo.setPayPropertyCustId(customId);
        try {
            commDimService.save(commDimBo);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public Map getDetailInfoForOrder(Order order) {
        Map map = new HashMap();
        /*map.put("车牌号", order.getAttach());
        map.put("停车场编号", order.getAttach2());
        CarPrivateInfo carPrivateInfo = JSON.parseObject(order.getDetail(), CarPrivateInfo.class);
        if (carPrivateInfo != null)
            map.put("到期时间", carPrivateInfo.getRenewExpirationTime());*/
        return map;
    }

    @Override
    public List<PayModuleInfo> getPayModuleInfo(String orderType,String commCode) {
        TblCommDimBo comm = commDimService.findTblCommDimById(commCode);
        PayModuleInfo payModuleInfo = new PayModuleInfo();
        payModuleInfo.setId(comm.getCommCode());
        payModuleInfo.setCustomId(comm.getPayPropertyCustId());
        payModuleInfo.setName(comm.getCommName());
        payModuleInfo.setOrderType(orderType);
        payModuleInfo.setOrderDesc(orderService.getModuleDesc(orderType));
        List list = new ArrayList();
        list.add(payModuleInfo);

        return list;
    }
}
